// index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Word: '',//关键词
    CurrentPage: 1,
    Categorys: [],
    selCatId: 0,
    selCatName: '',
    Brands: [],
    selBrandId: 0,
    selBrandName: '',
    Prices: [],
    selPriceId: 0,
    selPriceName: '',
    isCategoryTap: false,
    isBrandTap: false,
    isPriceTap: false,
    showSearch: false,
    searchCategory: [],//当前搜索类型
    goodsResult: [],
    loadingMoreHidden: true,
    storeId:0

  },
  toDetailsTap: function (e) {
    var that = this
    var pid = e.currentTarget.dataset.id
    //获取拼团信息
    wx: wx.request({
      url: app.globalData.host + '/api/Store/GetGroupBuyProductInfo?storeId=' + app.globalData.storeId + '&pid=' + pid,
      success: function (res) {
        if (res.data.Code == 0) {
          wx.navigateTo({
            url: '/pages/pintuan-details/index?id=' + pid,
          })
        } else {
          wx.navigateTo({
            url: "/pages/goods-details/index?id=" + pid
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      storeId:app.globalData.storeId
    })
    wx.setNavigationBarTitle({
      title: wx.getStorageSync('mallName')
    })
    var prices = [{ id: 0, name: '不限制'},{ id: 1, name: '0-100' }, { id: 2, name: '100-300' }, { id: 3, name: '300-600' }, { id: 4, name: '600-1000' }, { id: 5, name: '1000-2000' }];
    that.setData({ Prices: prices});
    wx.request({
      url: app.globalData.host + '/api/store/GetStoreClassList',
      data: {
        storeId: app.globalData.storeId
      },
      success: function (res) {
        var categories = [{ id: 0, name: "不限制", parentid: 0 }];
        for (var i = 0; i < res.data.Data.length; i++) {
          var temp = res.data.Data[i];
            var c = {};
            c.parentid = temp.ParentId
            c.id = temp.StoreCid;
            c.name = temp.Name;
            categories.push(c);
        }
        if (options.id || options.name){
          var selCatName = options.name
          var selCatId = options.id
        }else{
          var selCatName = ""
          var selCatId = 0
        }
        that.setData({
          Categorys: categories,
          selCatName: selCatName,
          selCatId: selCatId
        });
        that.searchGoods();
      }
    })
  },
  searchGoods: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    var priceStart=0;
    var priceEnd=0;
    if (that.data.selPriceId == 1) {priceStart=0;priceEnd=100;}
    else if (that.data.selPriceId == 2){priceStart=100;priceEnd=300}
    else if (that.data.selPriceId == 3) { priceStart = 300; priceEnd = 600 }
    else if (that.data.selPriceId == 4) { priceStart = 600; priceEnd = 1000 }
    else if (that.data.selPriceId == 5) { priceStart = 1000; priceEnd = 2000 }
    wx.request({
      url: app.globalData.host + '/api/store/Search',
      data: {
        storeId: app.globalData.storeId,
        cateId: that.data.selCatId,
        word: that.data.Word,
        page: that.data.CurrentPage,
        pageSize: 8,
        priceStart: priceStart,
        priceEnd: priceEnd
      },
      success: function (res) {
        wx.hideLoading()
        if(res.data.Code == 0){
          var newbs = [];
          var rawData = res.data.Data;
          var products = rawData.ProductList;
          if (that.data.CurrentPage != 1) {
            var preGoods = that.data.goodsResult
            newbs = preGoods.concat(newbs);
          }
          for (var i = 0; i < products.length; i++) {
            var d = products[i];
            if (d.SKUGid == 0) {
              d.ShowImg = app.globalData.image800_800host + d.ShowImg;
              newbs.push(d);
            } else {
              if (!that.checkProduct(newbs, d.SKUGid)) {
                d.ShowImg = app.globalData.image800_800host + d.ShowImg;
                newbs.push(d);
              }
            }
          }
          that.setData({
            goodsResult: newbs,
            loadingMoreHidden: rawData.PageModel.HasNextPage
          });
        }
      }
    })
  },
  //sku产品去重
  checkProduct: function (products, text) {
    var state = false;  //默认不存在
    if (products.length > 0 & text != 0) {
      if (products.length > 0) {
        for (var i = 0; i < products.length; i++) {
          if (text == products[i].SKUGid) {
            state = true; //已经存在
            break;
          }
        }
      }
    }
    return state;
  },
  /** 点击类别外的区域，取消显示类别信息 */
  handleCoverTap: function (e) {
    this.setData({ "showSearch": false });
  },
  /** 重置标签页状态 */
  resetMenuTap: function (menu) {
    var readyData = { "isCategoryTap": false, "isBrandTap": false, "isPriceTap": false };
    this.setData(readyData);
  },
  handleCategoryTap: function (e) {
    if (this.data.showSearch == false){
      this.setData({ "searchCategory": this.data.Categorys, "showSearch": true });
    } else if(this.data.showSearch == true){
      this.setData({ "searchCategory": this.data.Categorys, "showSearch": false });
    }
    this.resetMenuTap();
    this.setData({ "isCategoryTap": true });
  },
  handlePriceTap: function (e) {
    this.setData({ "searchCategory": this.data.Prices, "showSearch": true });
    this.resetMenuTap();
    this.setData({ "isPriceTap": true });
  },
  handleBrandTap: function (e) {
    this.setData({ "searchCategory": this.data.Brands, "showSearch": true });
    this.resetMenuTap();
    this.setData({ "isBrandTap": true });
  },
  /** 点击某个子类型 */
  handleSearch: function (event) {
    var id = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.name;
    var readyData = { "showSearch": false, CurrentPage: 1 };
    this.data.isCategoryTap && (readyData["selCatId"] = id);
    this.data.isCategoryTap && (readyData["selCatName"] = name);
    this.data.isBrandTap && (readyData["selBrandId"] = id);
    this.data.isBrandTap && (readyData["selBrandName"] = name);
    this.data.isPriceTap && (readyData["selPriceId"] = id);
    this.data.isPriceTap && (readyData["selPriceName"] = name);
    readyData["goodsResult"] = [];
    this.setData(readyData);
    this.searchGoods();
    this.resetMenuTap();
  },
  loadMore: function (e) {
    var that = this;
    if (!that.data.loadingMoreHidden) return;
    var currPage = that.data.CurrentPage;
    that.setData({
      CurrentPage: currPage + 1
    })
    that.searchGoods();
  },
  /** 点击完成按钮是触发 */
  handleConfirm: function (e) {
    this.setData({ Word: e.detail.value })
  },
  /** 搜索框失去焦点是触发 */
  handleBlur: function (e) {
    this.setData({ Word: e.detail.value })
  },
  /** 取消搜索 */
  handleCancel: function (e) {
    this.setData({ Word: '' })
  },
  handleInput: function (e) {
    this.setData({ Word: e.detail.value })
  },
  handleSearchWord: function (e) {
    this.setData({
      CurrentPage: 1,
      selCatId: 0,
      selCatName: '',
      selBrandId: 0,
      selBrandName: '',
      selPriceId: 0,
      selPriceName: '',
      isCategoryTap: false,
      isBrandTap: false,
      isPriceTap: false,
      showSearch: false,
      searchCategory: [],//当前搜索类型
      goodsResult: [],
      loadingMoreHidden: false
    })
    this.searchGoods();
  },
  onPullDownRefresh: function () {
    var that = this;
    wx.showToast({
      title: '正在刷新',
      duration: 1500,
      icon: 'loading',
      success: function () {
        that.setData({ CurrentPage: 1 });
        that.searchGoods();
      },
      complete: function () {
        wx.stopPullDownRefresh();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})